package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	
	import flash.net.URLRequest;
	
	import org.flexunit.async.Async;
	
	//[RunWith("org.flexunit.experimental.theories.Theories")]
	public class SocialGraphTest
	{
		private static const FRIENDS_URL_BASE:String = "http://api.t.sina.com.cn/friends/ids";
		private static const FOLLOWERS_URL_BASE:String = "http://api.t.sina.com.cn/followers/ids";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var tester:String = "tester";
		
		[DataPoints]
		[ArrayElementType("uint")]
		public static var ids:Array = [0, 12213213];
		
		[DataPoints]
		[ArrayElementType("int")]
		public static var cursors:Array = [-1, 0, 1];
		
		[DataPoints]
		[ArrayElementType("String")]
		public static var names:Array = [ null, "t测试a"];
		
		public function SocialGraphTest()
		{
		}
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		
		// Test Friend id list.
		[Test(async,timeout="20000")]
		public function testFriendIds():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_ID_LIST_RESULT, AssertResult.assertIDList, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FRIENDS_URL_BASE + "/" + tester + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsIDList(tester);
			URLRequestChecker.check(mb, req, ResultDataUtils.idList);
		}
		[Test(async,timeout="20000")]
		public function testFriendIdsWithoutUser():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_ID_LIST_RESULT, AssertResult.assertIDList, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FRIENDS_URL_BASE + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsIDList(null);
			URLRequestChecker.check(mb, req, ResultDataUtils.idList);
		}
		[Test(async,timeout="20000")]
		public function testFriendIdsFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FRIENDS_ID_LIST_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FRIENDS_URL_BASE + "/" + tester + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsIDList(tester);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
//		[Theory]
//		public function testFriendIdsTheory(userID:uint, screenName:String, cursor:int, count:uint):void
//		{
//			var params:URLRequestParameters = new URLRequestParameters(mb.source);
//			if ( userID > 0 )
//			{
//				params.addParam("user_id", userID);
//			}
//			if ( null != screenName )
//			{
//				params.addParam("screen_name", screenName);
//			}
//			if ( cursor > -1 )
//			{
//				params.addParam("cursor", cursor);
//			}
//			var url:String = FRIENDS_URL_BASE + ".xml" + params.toGetString();
//			mb.loadFriendsIDList(null, userID, screenName, cursor, count);
//		}
		
		// Test Followers id list.
		[Test(async,timeout="20000")]
		public function testFollowersIds():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FOLLOWERS_ID_LIST_RESULT, AssertResult.assertIDList, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FOLLOWERS_URL_BASE + "/" + tester + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersIDList(tester);
			URLRequestChecker.check(mb, req, ResultDataUtils.idList);
		}
		[Test(async,timeout="20000")]
		public function testFollowersIdsFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FOLLOWERS_ID_LIST_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FOLLOWERS_URL_BASE + "/" + tester + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersIDList(tester);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Test(async,timeout="20000")]
		public function testFollowersIdsWithoutUser():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FOLLOWERS_ID_LIST_RESULT, AssertResult.assertIDList, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("count", 5000);
			var url:String = FOLLOWERS_URL_BASE + ".xml" + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadFollowersIDList(null);
			URLRequestChecker.check(mb, req, ResultDataUtils.idList);
		}
//		[Theory]
//		public function testFollowersTheory(userID:uint, screenName:String, cursor:int, count:uint):void
//		{
//			var params:URLRequestParameters = new URLRequestParameters(mb.source);
//			if ( userID > 0 )
//			{
//				params.addParam("user_id", userID);
//			}
//			if ( null != screenName )
//			{
//				params.addParam("screen_name", screenName);
//			}
//			if ( cursor > -1 )
//			{
//				params.addParam("cursor", cursor);
//			}
//			var url:String = FOLLOWERS_URL_BASE + ".xml" + params.toGetString();
//			mb.loadFollowersIDList(null, userID, screenName, cursor, count);
//		}
	}
}